#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Usage.hpp"
#include "Server.hpp"
#include "Util.hpp"
// void HandlerHttpRequest(int sockfd)
// {
    
// }
#define ROOT "./wwwroot" // ./wwwroot/index.html
void HandlerHttpRequest(int sockfd)
{
    // 1. 读取请求 for test
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
    }
    std::cout << buffer << "\n--------------------\n" << std::endl;

    std::vector<std::string> vline;
    Util::cutString(buffer, "\n", &vline);

    std::vector<std::string> vblock;
    Util::cutString(vline[0], " ", &vblock);

    std::string file = vblock[1]; // /a/b/c.html
    std::string target = ROOT;

    if(file == "/") file = "/index.html";
    // wwwroot/index.html
    // wwwroot/a/b/c.html
    target += file;
    std::cout << target << std::endl;

    std::string content;
    std::ifstream in(target);
    if(in.is_open())
    {
        std::string line;
        while(std::getline(in, line))
        {
            content += line;
        }
        in.close();
    }

    std::string HttpResponse;
    if(content.empty()) 
    {
        HttpResponse = "HTTP/1.1 301 Moved Permanently\r\n";
        HttpResponse += "Location: http://120.78.126.148:8081/a/b/404.html\r\n";
    }
    else 
    {
        HttpResponse = "HTTP/1.1 200 OK\r\n";
        HttpResponse += ("Content-Type: text/html\r\n"); 
        HttpResponse += ("Content-Length: " + std::to_string(content.size()) + "\r\n");
        HttpResponse += "Set-Cookie: 这是一个cookie\r\n";
    }
    HttpResponse += "\r\n";
    HttpResponse += content;
    // std::cout << "####start################" << std::endl;
    // for(auto &iter : vblock)
    // {
    //     std::cout << "---" << iter << "\n" << std::endl;
    // }
    // std::cout << "#####end###############" << std::endl;

    // 2. 试着构建一个http的响应
    send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::unique_ptr<Server> httpserver(new Server(atoi(argv[1]), HandlerHttpRequest));
    httpserver->Start();
    return 0;
}


// int main()
// {
//     Sock socket;
//     int sock=socket.Socket();
//     socket.Bind(sock,8080);
//     socket.Listen(sock);

//     while(true)
//     {
//         std::string clientip;
//         uint16_t clientport;
//         int sockfd = socket.Accept(sock,&clientport,&clientip);
//         if(sockfd > 0)
//         {
//             std::cout << "[" << clientip << ":" << clientport << "]# " << sockfd << std::endl;;
//         }
//     }
//     return 0;
// }